//
//  EAppSetView.m
//  AppRanking
//
//  Created by Gang.Wang on 12-11-9.
//  Copyright (c) 2012年 Gang.Wang. All rights reserved.
//

#import "EAppSetView.h"
#import "mcf.h"
#import "EAppLogoView.h"
#import "EInputApp.h"
#import "EGlobalAppData.h"
#import "JSONKit.h"

@interface EAppSetView()<EInputAppDelegate, EAppLogoViewDelegate>
{
    UIScrollViewEx * contentScrollView_;
    NSMutableArray * appViewArray_;
    
    UIButton * addSpyAppButton_;
    EInputApp * inputAppView_;
    
    BOOL isSplitModel;
    NSMutableArray * appInfoArray_;
}

@property (nonatomic, strong) UIScrollViewEx * contentScrollView;
@property (nonatomic, strong) NSMutableArray * appViewArray;

@property (nonatomic, strong) UIButton * addSpyAppButton;
@property (nonatomic, strong) EInputApp * inputAppView;

@property (nonatomic, strong) NSMutableArray * appInfoArray;


@end


@implementation EAppSetView

@synthesize contentScrollView = contentScrollView_;
@synthesize appIdArray = appIdArray_;

@synthesize appViewArray = appViewArray_;
@synthesize addSpyAppButton = addSpyAppButton_;

@synthesize delegate = delegate_;
@synthesize inputAppView = inputAppView_;

@synthesize appInfoArray = appInfoArray_;

- (void) dealloc
{
    YRelease(contentScrollView_)
    YRelease(appIdArray_)
    
    YRelease(addSpyAppButton_)
    YRelease(inputAppView_)
    
    YRelease(appInfoArray_)
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (self)
    {
        [self privateInit];
    }
    
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        [self privateInit];
    }
    return self;
}


- (void) privateInit
{
    if (self.bInited)
    {
        return;
    }
    
    [self setBackgroundColor:COLOR_SCROLLVIEWTEXTUREDBACKGROUND];
    
    NSMutableArray * temp_viewArray = [[NSMutableArray alloc] init];
    self.appViewArray = temp_viewArray;
    YRelease(temp_viewArray);
    
    
    EInputApp * temp_inputAppView = [[EInputApp alloc] init];
    [temp_inputAppView setDelegate:self];
    
    self.inputAppView = temp_inputAppView;
    YRelease(temp_inputAppView)
    [self addSubview:self.inputAppView];
    
    
    
    UIScrollViewEx * temp_contentView = [[UIScrollViewEx alloc] init];
    [temp_contentView setBackgroundColor:COLOR_SCROLLVIEWTEXTUREDBACKGROUND];
    self.contentScrollView = temp_contentView;
    YRelease(temp_contentView)
    
    [self addSubview:self.contentScrollView];
    
    
    
    self.addSpyAppButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    BUTTON_FUNCTION(self.addSpyAppButton, addAppSpy);
//    [self addSubview:self.addSpyAppButton];
 
//    [self showDefaultSpyApps];
    
    [self reAdjustLayout];
    self.bInited = YES;
    
    [self performSelector:@selector(showDefaultSpyApps) withObject:nil afterDelay:1.0f];
}

- (void) showDefaultSpyApps
{
    NSArray * spyAppArray = [[EGlobalAppData sharedInstance] spyAppArray];
    if ([NSArray isNilOrEmpty:spyAppArray])
    {
        return;
    }
    
    for (NSString * appId in spyAppArray)
    {
        if ([NSString isNilOrEmpty:appId])
        {
            continue;
        }
        
        NSString * appInfoDataFilePath = [EGlobalAppData appDataFilePath:appId];
        BOOL needToDownload = NO;
        if ([YFile fileExistsAtPath:appInfoDataFilePath])
        {
            NSDictionary * dic_data = [NSDictionary dictionaryWithContentsOfFile:appInfoDataFilePath];
            
            NSData * data = [dic_data JSONData];
            if (data)
            {
                EAppInfo * appInfo = [[EAppInfo alloc] initWithJsonData:data];
                [self showNewApp:appInfo];
                YRelease(appInfo)
            }
            else
            {
                needToDownload = NO;
            }
        }
        else
        {
            needToDownload = YES;
        }
            
    }
        
}


- (void) reAdjustLayout
{
    [self.addSpyAppButton setFrame:CGRectMake(0, 0, 40, 40)];
    
    if (isSplitModel)
    {
        [self.contentScrollView setFrame:CGRectMake(0, self.height - 5, self.width, self.height)];
        [self.inputAppView setFrame:self.bounds];
    }
    else
    {
        [self.contentScrollView setFrame:self.bounds];
        [self.inputAppView setFrame:self.bounds];
    }
    
    
    float itemViewWidth = 150;
    float origin_x = 0.0f;
    for (EAppLogoView * appView in self.appViewArray)
    {
        CGRect frame = CGRectMake(origin_x, 0, itemViewWidth, self.contentScrollView.height);
        origin_x += itemViewWidth;
        
        [appView setFrame:frame];
    }
}


- (void) setAppIdArray:(NSArray *)appIdArrayValue
{
    YRelease(appIdArray_)
    appIdArray_ = [appIdArrayValue retain];
    
    [self showAllAppToView];
}


- (void) showAllAppToView
{
    [self.appViewArray removeAllObjects];
    
    for (NSString * appId in self.appIdArray)
    {
        if ([NSString isNilOrEmpty:appId])
        {
            continue;
        }
        
        EAppLogoView * appLogoView = [[EAppLogoView alloc] init];

        [appLogoView setDelegate:self];
        
        [self.contentScrollView addSubview:appLogoView];
        [self.appViewArray addObject:appLogoView];
        YRelease(appLogoView)
    }
}


- (void) addAppSpy
{
    isSplitModel = !isSplitModel;
    [[YkAnimationStep for:0.2f animate:^{
        [self reAdjustLayout];
    }] runAnimation];
}


- (void) showApp:(EAppInfo *)appInfo
{
    if (!appInfo)
    {
        return;
    }
    
    if (isSplitModel)
    {
        isSplitModel = !isSplitModel;
        [[YkAnimationStep for:0.2f animate:^{
            [self reAdjustLayout];
        }] runAnimation];
    }
    
    
    if (![self isShowedApp:appInfo])
    {
        [self.appInfoArray addObject:appInfo];
        [self showNewApp:appInfo];
    }
    else
    {
        [self removeAPpInfoFromAppInfoArray:appInfo.appId];
        [self.appInfoArray addObject:appInfo];
        
        [self refreshApp:appInfo];
    }
}


- (void) showNewApp:(EAppInfo *) app
{
    if (!app)
    {
        return;
    }

    EAppLogoView * appLogoView = [[EAppLogoView alloc] init];
    [appLogoView setDelegate:self];

    [appLogoView setAppData:app];

    [self.appViewArray addObject:appLogoView];
    [self.contentScrollView addSubview:appLogoView];

    YRelease(appLogoView)

    [self reAdjustLayout];
}


- (void) refreshApp:(EAppInfo *) app
{
    if (!app)
    {
        return;
    }
    
    
}

- (BOOL) isShowedApp:(EAppInfo *) app
{
    return [self isShowedAppId:app.appId];
}


- (BOOL) isShowedAppId:(NSString *) appId
{
    BOOL result = NO;
    
    for (EAppInfo * app in self.appInfoArray)
    {
        if ([app.appId isEqualToString:appId])
        {
            result = YES;
            break;
        }
    }    
    
    return result;
}

- (void) removeAPpInfoFromAppInfoArray:(NSString *) appId
{
    EAppInfo * needToDeleteobj = nil;
    for (EAppInfo * app in self.appInfoArray)
    {
        if ([app.appId isEqualToString:appId])
        {
            needToDeleteobj = app;
            break;
        }
    }
    
    
    if (needToDeleteobj)
    {
        [self.appInfoArray removeObject:needToDeleteobj];
    }
}


- (void) addAppToSpy:(NSString *)appid
{
    if ([NSString isNilOrEmpty:appid])
    {
        return;
    }
    
    DELEGATE_CALLBACK(addAppToSpy:, appid);
}


- (NSMutableArray *) appInfoArray
{
    if (!appInfoArray_)
    {
        appInfoArray_ = [[NSMutableArray alloc] init];
    }
    
    
    return appInfoArray_;
}


#pragma mark - EAppLogoViewDelegate
- (void) need2Download:(DownloadRequest *)request
{
    if (!request)
    {
        return;
    }
        
    DELEGATE_CALLBACK(need2Download:, request)
}

- (void) appClicked:(EAppInfo *) appInfo
{
    if (!appInfo)
    {
        return;
    }
    
    DELEGATE_CALLBACK(appClicked:, appInfo);
}

@end
